package com.gtw.modules.api.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.AuthIgnoreApi;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.dto.TbCommodityDto;
import com.gtw.modules.api.service.TbCommodityPropertyService;
import com.gtw.modules.api.service.TbCommodityService;
import com.gtw.modules.api.service.TbPropertyService;
import com.gtw.modules.entity.TbCommodity;
import com.gtw.modules.entity.TbCommodityProperty;
import com.gtw.modules.entity.TbProperty;
import com.gtw.utils.EntityDtoConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 商品表
 *
 * @author wuye
 * @email www.baidu.com
 * @date 2018-02-01 10:46:46
 */
@RestController
@RequestMapping("/api/commodity")
public class TbCommodityController {
    @Autowired
    private TbCommodityService commodityService;

    @Autowired
    private TbCommodityPropertyService tbCommodityPropertyService;

    @Autowired
    private TbPropertyService tbPropertyService;

    /**
     * 列表
     */
    @AuthIgnoreApi
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<TbCommodity> pageUtil = new Page<TbCommodity>(query.getPage(), query.getLimit());
        Page<TbCommodity> page = commodityService.queryPageList(pageUtil, query);
        Page<TbCommodityDto> dtoPage = new Page<TbCommodityDto>();
        List<TbCommodityDto> tbCommodityDtoList = new ArrayList<>();
        EntityDtoConverter edc = new EntityDtoConverter();
        for (int i = 0; i < page.getRecords().size(); i++) {
            TbCommodityDto dto = new TbCommodityDto();
            try {
                dto = (TbCommodityDto) edc.entityConvertDto(page.getRecords().get(i), dto);
                Map<String, Object> map = new HashMap<>();
                map.put("commodityId", dto.getId());
                List<TbCommodityProperty> tbCommodityPropertyList = tbCommodityPropertyService.findInfo(map);
                List<TbProperty> tbPropertyList = new ArrayList<>();
                for (int j = 0; j < tbCommodityPropertyList.size(); j++) {
                    TbProperty tbProperty = tbPropertyService.queryObject(tbCommodityPropertyList.get(j).getPropertyid());
                    tbProperty.setPropertynum(tbCommodityPropertyList.get(j).getPropertynum());
                    tbPropertyList.add(tbProperty);
                }
                dto.setTbPropertyList(tbPropertyList);
                tbCommodityDtoList.add(dto);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        dtoPage.setRecords(tbCommodityDtoList);
        return R.ok().put("data", dtoPage);
    }


    /**
     * 信息
     */
    @AuthIgnoreApi
    @RequestMapping("/info")
    public R info(Integer id) {
        TbCommodity commodity = commodityService.queryObject(id);
        TbCommodityDto dto = new TbCommodityDto();
        EntityDtoConverter edc = new EntityDtoConverter();
        try {
            dto = (TbCommodityDto) edc.entityConvertDto(commodity, dto);
            Map<String, Object> map = new HashMap<>();
            map.put("commodityId", dto.getId());
            List<TbCommodityProperty> tbCommodityPropertyList = tbCommodityPropertyService.findInfo(map);
            List<TbProperty> tbPropertyList = new ArrayList<>();
            for (int j = 0; j < tbCommodityPropertyList.size(); j++) {
                TbProperty tbProperty = tbPropertyService.queryObject(tbCommodityPropertyList.get(j).getPropertyid());
                tbProperty.setPropertynum(tbCommodityPropertyList.get(j).getPropertynum());
                tbPropertyList.add(tbProperty);
            }
            dto.setTbPropertyList(tbPropertyList);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return R.ok().put("data", dto);
    }


}
